package com.acmedcare.framework.exchange.center.core.storage;

import com.acmedcare.framework.exchange.center.core.Const;
import com.acmedcare.framework.exchange.center.entity.bean.Plugin;
import com.acmedcare.framework.exchange.center.entity.bean.PluginStatusEnum;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PluginRepository {

  final JdbcTemplate jdbcTemplate;

  public PluginRepository(JdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
  }

  public List<Plugin> getPlugins() {

    try {

      List<Plugin> plugins = jdbcTemplate
          .query("select * from center_plugin", new PluginRowMapper());
      return plugins;
    } catch (EmptyResultDataAccessException e) {

      log.error("获取插件数据异常", e);
      return null;
    }
  }

  public Plugin getPlugin(String name) {
    try {

      Plugin plugin = jdbcTemplate
          .queryForObject("SELECT * FROM center_plugin WHERE `name`=?", new Object[]{name},
              new
                  PluginRowMapper());

      return plugin;
    } catch (EmptyResultDataAccessException e) {

      log.error("获取插件异常", e);
      return null;
    }
  }

  public void delete(Long pluginId) {

    String sql = "delete from center_plugin where plugin_id=?";
    jdbcTemplate.update(sql, new Object[]{pluginId});
  }

  public void insert(Plugin plugin) {

    String sql = "INSERT INTO center_plugin (plugin_id,base_package_name,description,jar_name,version,`name`,group_name,`status`,jar_path,spring_boot) VALUES(?,?,?,?,?,?,?,?,?,?)";

    try {
      jdbcTemplate.update(sql,
          new Object[]{plugin.getPluginId(), plugin.getBasePackageName(), plugin.getDescription()
              , plugin.getJarName(), plugin.getVersion(), plugin.getName(), System.getenv(
              Const.GroupName),
          plugin.getStatus().toString(),
          plugin.getJarPath(),plugin.getSpringBoot()});
    } catch (EmptyResultDataAccessException e) {
      log.error("插入插件异常!", e);
    }
  }

  class PluginRowMapper implements RowMapper<Plugin> {

    @Override
    public Plugin mapRow(ResultSet resultSet, int i) throws SQLException {

      Plugin plugin = new Plugin();
      plugin.setPluginId(resultSet.getLong("plugin_id"));
      plugin.setBasePackageName(resultSet.getString("base_package_name"));
      plugin.setDescription(resultSet.getString("description"));
      plugin.setJarName(resultSet.getString("jar_name"));
      plugin.setVersion(resultSet.getString("version"));
      plugin.setName(resultSet.getString("name"));
      plugin.setGroup_name(resultSet.getString("group_name"));
      plugin.setStatus(PluginStatusEnum.valueOf(resultSet.getString("status")));
      plugin.setJarPath(resultSet.getString("jar_path"));
      plugin.setSpringBoot(resultSet.getBoolean("spring_boot"));

      return plugin;
    }
  }
}
